package com.iwe3.protal.config;

import com.iwe3.protal.handler.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * SpringMVC的配置，必须实现：WebMvcConfigurer 接口
 * 替代：spring-mvc.xml
 */
@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Autowired
    @Qualifier("loginInterceptor")
    private LoginInterceptor loginInterceptor;

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //http://localhost:8080/  -> index
        registry.addViewController("/").setViewName("index");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        /*定义：白名单*/
        List<String> whiteList = new ArrayList<>();
        Collections.addAll(whiteList,"/static/**",
                "/","/druid/**",
                "/page/**","/index.html","/login.html",
                "/user/login","/book/**","/bookType/**",
                "/swagger-ui.html","/swagger-ui/**","/v3/api-docs/**");
        /*向MVC框架对应的IOC容器，注册：登录拦截器*/
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(whiteList);
    }
}